﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class buy_Device : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(buy_Device));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.Devfile.ToString();//"Devfile";

        #region 载入信息

        public void loadInfo()
        {
            BuyDevice buy = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                buy = new BuyDevice();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(253, ref sModelName, (clsBaseLib)buy))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(252, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(253, ref sModelName, (clsBaseLib)buy))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = buy.getDeviceInfoById(m_Id);
                    //hdselect.Value = buy.getDeviceSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtDeviceCode.Text = Convert.ToString(dt.Rows[0]["DeviceCode"]);
                        txtDeviceName.Text = Convert.ToString(dt.Rows[0]["DeviceName"]);
                        txtSpec.Text = Convert.ToString(dt.Rows[0]["Devicespec"]);
                        txtDevTypeName.Text = Convert.ToString(dt.Rows[0]["TypeName"]);
                        txtDevTypeId.Text = Convert.ToString(dt.Rows[0]["TypeId"]);

                        txtAccDate.Text = Convert.ToDateTime(dt.Rows[0]["BuyDateTime"]).ToString("yyyy-MM-dd");
                        //txtCompanyName.Text = Convert.ToString(dt.Rows[0]["companyname"]);
                        txtComment.Text = Convert.ToString(dt.Rows[0]["Comment"]);
                        //txtStartDate.Text = Convert.ToDateTime(dt.Rows[0]["startdate"]).ToString("yyyy-MM-dd");
                        //txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["enddate"]).ToString("yyyy-MM-dd");
                        //txtMemo.Text = Convert.ToString(dt.Rows[0]["memo"]);
                        txtUseDeptName.Text = Convert.ToString(dt.Rows[0]["DeptName"]);
                        txtUseDeptId.Text = Convert.ToString(dt.Rows[0]["DeptId"]);
                        if (Convert.ToInt32(dt.Rows[0]["DeptId"]) != 0)
                        {
                            btnUseDept.Visible = false;
                        }

                        txtAccDeptName.Text = Convert.ToString(dt.Rows[0]["AccDeptName"]);
                        txtAccDeptId.Text = Convert.ToString(dt.Rows[0]["AccDeptId"]);

                        txtUserName.Text = Convert.ToString(dt.Rows[0]["AccUserName"]);
                        //txtPayMoney.Text = Convert.ToString(dt.Rows[0]["AccUserName"]);
                        //txtContent.SelectedValue = Convert.ToString(dt.Rows[0]["Comment"]);
                        //tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["Devicefile"]);
                    }
//                    dt = ds.Tables[1];
//                    int iRowCount = dt.Rows.Count;
//                    StringBuilder sbu = new StringBuilder();
//                    for (int i = 0; i < iRowCount; i++)
//                    {
//                        sbu.Append(string.Format(@"<tr>
//                        <td align=""left"" width=""120"">
//                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
//                        <td width=""150"">
//                            <input style=""width: 90px"" type=""text"" value=""{4}"" /></td>
//                    </tr>", dt.Rows[i]["typename"], dt.Rows[i]["typeid"], dt.Rows[i]["devicenum"], dt.Rows[i]["singleprice"], dt.Rows[i]["modelinfo"]));
//                    }
//                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        [Ajax.AjaxMethod]
        public DataTable getSelectDeviceInfo(string sIds)
        {
            BuyDevice buy = null;
            DataTable dt = null;
            try
            {
                buy = new BuyDevice();
                dt = buy.getResultDeviceInfo(sIds);
            }
            catch
            {
                dt = null;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return dt;
        } 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int idevid, string sdevcode, string sdevdate, string sdevname,int idevtype, int iuserdept, string sdevspec, int iaccdeptid, string saccusername,string sComment)
        {
            BuyDevice buy = null;
            bool bResult = false;
            sdevcode = sdevcode.Replace("'", "''");
            sdevdate = sdevdate.Replace("'", "''");
            sdevname = sdevname.Replace("'", "''");
            sdevspec = sdevspec.Replace("'", "''");
            saccusername = saccusername.Replace("'", "''");
            sComment = sComment.Replace("'", "''");
            try
            {
                buy = new BuyDevice();

                //for (int i = 0; i < sDevices.Length; i++)
                //{
                //    sDevices[i] = sDevices[i].Replace("'", "''");
                //}
                bResult = buy.setDeviceInfo(idevid, sdevcode, sdevdate, sdevname, idevtype, iuserdept, sdevspec, iaccdeptid, saccusername, sComment);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (buy != null)
                {
                    buy.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}